package demo.controller;

import demo.model.Stock;
import demo.service.StockService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 此类为使用StockService的控制器类，接受http请求并返回
 */
@RestController
public class ControllerForStockService {
    final private StockService stockService;

    @Autowired
    public ControllerForStockService(StockService stockService){
        this.stockService = stockService;
    }

    @RequestMapping("/getStockByName/{name}")
    public List<Stock> getStockByName(@PathVariable String name){
        return stockService.findByName(name);
    }

    @RequestMapping("/getAllStocks")
    public List<Stock> getAllStocks(){
        return stockService.findAll();
    }

    @RequestMapping("/deleteStock")
    public void deleteStock(){
        stockService.delete();
    }

    @RequestMapping("/deleteStockById/{id}")
    public void deleteStockById(@PathVariable String id){
        //注意这里做一个类型转换，url接收参数应为String
        stockService.deleteStockById(Integer.valueOf(id));
    }

    @RequestMapping("/insertStock")
    public Stock insertStock(){
        return stockService.insertStock();
    }

    @RequestMapping("/updateStock")
    public Stock updateStock(){
        return stockService.updateStock();
    }

}
